/***************************************************************
 * Name:      ChemistryTestMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    xuzl (smartbirdxzl@hotmail.com)
 * Created:   2010-02-26
 * Copyright: xuzl (www.cnblogs.com/xuzhenling)
 * License:
 **************************************************************/

#include "ChemistryTestMain.h"
#include <wx/msgdlg.h>
#include "sqlite3\sqlite3.h"

//(*InternalHeaders(ChemistryTestFrame)
#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

#include "AddQuestionFrame.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(ChemistryTestFrame)
const long ChemistryTestFrame::ID_STATICTEXT1 = wxNewId();
const long ChemistryTestFrame::ID_STATICTEXT2 = wxNewId();
const long ChemistryTestFrame::ID_TEXTCTRL1 = wxNewId();
const long ChemistryTestFrame::ID_BUTTON1 = wxNewId();
const long ChemistryTestFrame::ID_STATICTEXT3 = wxNewId();
const long ChemistryTestFrame::ID_TEXTCTRL2 = wxNewId();
const long ChemistryTestFrame::ID_STATICTEXT4 = wxNewId();
const long ChemistryTestFrame::ID_PANEL1 = wxNewId();
const long ChemistryTestFrame::idMenuQuit = wxNewId();
const long ChemistryTestFrame::ID_MENUITEM1 = wxNewId();
const long ChemistryTestFrame::ID_MENUITEM2 = wxNewId();
const long ChemistryTestFrame::ID_MENUITEM3 = wxNewId();
const long ChemistryTestFrame::ID_MENUITEM4 = wxNewId();
const long ChemistryTestFrame::idMenuAbout = wxNewId();
const long ChemistryTestFrame::ID_STATUSBAR1 = wxNewId();
const long ChemistryTestFrame::ID_TOOLBARITEM1 = wxNewId();
const long ChemistryTestFrame::ID_TBI_AddQuestion = wxNewId();
const long ChemistryTestFrame::ID_TOOLBARITEM3 = wxNewId();
const long ChemistryTestFrame::ID_TOOLBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(ChemistryTestFrame,wxFrame)
    //(*EventTable(ChemistryTestFrame)
    //*)
END_EVENT_TABLE()

ChemistryTestFrame::ChemistryTestFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(ChemistryTestFrame)
    wxBoxSizer* BoxSizer4;
    wxBoxSizer* BoxSizer6;
    wxBoxSizer* BoxSizer5;
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxBoxSizer* BoxSizer3;
    wxMenu* Menu2;

    Create(parent, id, _("花园试题系统"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SetClientSize(wxSize(800,600));
    Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(96,136), wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("试题检索"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    BoxSizer2->Add(StaticText1, 1, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    BoxSizer1->Add(BoxSizer2, 0, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    BoxSizer3 = new wxBoxSizer(wxVERTICAL);
    BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
    StaticText2 = new wxStaticText(Panel1, ID_STATICTEXT2, _("关键词："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
    BoxSizer5->Add(StaticText2, 0, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    TextCtrl1 = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    BoxSizer5->Add(TextCtrl1, 2, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    Button1 = new wxButton(Panel1, ID_BUTTON1, _("出发！"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    BoxSizer5->Add(Button1, 1, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    BoxSizer3->Add(BoxSizer5, 0, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    BoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
    StaticText3 = new wxStaticText(Panel1, ID_STATICTEXT3, _("过滤条件："), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
    BoxSizer6->Add(StaticText3, 1, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    TextCtrl2 = new wxTextCtrl(Panel1, ID_TEXTCTRL2, _("文本"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
    BoxSizer6->Add(TextCtrl2, 1, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    BoxSizer3->Add(BoxSizer6, 1, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
    BoxSizer1->Add(BoxSizer3, 2, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    StaticText4 = new wxStaticText(Panel1, ID_STATICTEXT4, _("copyright"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT4"));
    BoxSizer4->Add(StaticText4, 1, wxALL|wxALIGN_LEFT|wxALIGN_TOP, 5);
    BoxSizer1->Add(BoxSizer4, 0, wxALL|wxALIGN_BOTTOM|wxALIGN_CENTER_HORIZONTAL, 5);
    Panel1->SetSizer(BoxSizer1);
    BoxSizer1->Fit(Panel1);
    BoxSizer1->SetSizeHints(Panel1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("退出\tAlt-F4"), _("退出应用程序"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("文件"));
    Menu3 = new wxMenu();
    MenuItem3 = new wxMenuItem(Menu3, ID_MENUITEM1, _("添加试题"), wxEmptyString, wxITEM_NORMAL);
    Menu3->Append(MenuItem3);
    MenuBar1->Append(Menu3, _("编辑"));
    Menu4 = new wxMenu();
    MenuItem4 = new wxMenuItem(Menu4, ID_MENUITEM2, _("组卷"), wxEmptyString, wxITEM_NORMAL);
    Menu4->Append(MenuItem4);
    MenuBar1->Append(Menu4, _("组卷"));
    Menu5 = new wxMenu();
    MenuItem5 = new wxMenuItem(Menu5, ID_MENUITEM3, _("新建试题库"), wxEmptyString, wxITEM_NORMAL);
    Menu5->Append(MenuItem5);
    MenuItem6 = new wxMenuItem(Menu5, ID_MENUITEM4, _("备份试题库"), wxEmptyString, wxITEM_NORMAL);
    Menu5->Append(MenuItem6);
    MenuBar1->Append(Menu5, _("题库"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("关于\tF1"), _("显示帮助信息"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("帮助"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    ToolBar1 = new wxToolBar(this, ID_TOOLBAR1, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxNO_BORDER, _T("ID_TOOLBAR1"));
    ToolBarItem1 = ToolBar1->AddTool(ID_TOOLBARITEM1, _("检索"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_FIND")),wxART_BUTTON), wxNullBitmap, wxITEM_NORMAL, _("检索"), _("检索试题"));
    TBIAddQuestion = ToolBar1->AddTool(ID_TBI_AddQuestion, _("添加"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_ADD_BOOKMARK")),wxART_BUTTON), wxNullBitmap, wxITEM_NORMAL, _("添加"), _("添加试题"));
    ToolBarItem3 = ToolBar1->AddTool(ID_TOOLBARITEM3, _("组卷"), wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_REPORT_VIEW")),wxART_BUTTON), wxNullBitmap, wxITEM_NORMAL, _("组卷"), _("生成试卷"));
    ToolBar1->Realize();
    SetToolBar(ToolBar1);
    Center();

    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ChemistryTestFrame::OnQuit);
    Connect(ID_MENUITEM3,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ChemistryTestFrame::OnCreateDataBaseSelected);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&ChemistryTestFrame::OnAbout);
    Connect(ID_TBI_AddQuestion,wxEVT_COMMAND_TOOL_CLICKED,(wxObjectEventFunction)&ChemistryTestFrame::OnTBIAddQuestionClicked);
    //*)
}

ChemistryTestFrame::~ChemistryTestFrame()
{
    //(*Destroy(ChemistryTestFrame)
    //*)
}

void ChemistryTestFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void ChemistryTestFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void ChemistryTestFrame::OnButtonOKClick(wxCommandEvent& event)
{
    wxString msg = _T("OK");
    wxMessageBox(msg, _("ok??"));
}


void ChemistryTestFrame::OnTBIAddQuestionClicked(wxCommandEvent& event)
{
  AddQuestionFrame* Frame = new AddQuestionFrame((wxWindow*)(event.GetEventObject()));
  Frame->Show();
}

void ChemistryTestFrame::OnCreateDataBaseSelected(wxCommandEvent& event)
{
    sqlite3 *ChemistryDB = NULL;
    int rc;
    wxString DataBasePath;
    DataBasePath = GetExePath() + _T("chemistryDB.db");
    rc = sqlite3_open(DataBasePath.mb_str(wxConvUTF8), &ChemistryDB);
    if (rc)
    {
      wxString msg = _T("Failed");
      wxMessageBox(msg, _("Create DataBase failed"));
      sqlite3_close(ChemistryDB);
    }

//    char* CreateTableSql = "create table TestTB(id integer primary key,"
//                           "                    num integer,"
//                           "                    context text)";
//    rc = sqlite3_exec(ChemistryDB, CreateTableSql, NULL, NULL, NULL);

//    char* InsertTableSql = "insert into TestTB(num, context) values (1, 'come on xuzl')";
//    rc = sqlite3_exec(ChemistryDB, InsertTableSql, NULL, NULL, NULL);
//
//    char* InsertTableSq2 = "insert into TestTB(num, context) values (2, 'come on baby')";
//    rc = sqlite3_exec(ChemistryDB, InsertTableSq2, NULL, NULL, NULL);

    char* QueryTableSql = "select * from ChemistryTest";
    char *CallBackData = "call back has been called";
    rc = sqlite3_exec(ChemistryDB, QueryTableSql, DBExecCallBack, CallBackData, NULL);

    sqlite3_close(ChemistryDB);

}

int DBExecCallBack(void *Data, int ColCount, char **ColValues, char **ColHeaders)
{
   int i;
   for(i=0;i<ColCount;i++)
   {
       wxString colTitle(ColHeaders[i], wxConvUTF8);
       wxMessageBox(colTitle);
   }
   return 0;
}
